<?php 
$this->pageTitle=Yii::app()->name . ' - Giỏ hàng của bạn';
$this->breadcrumbs=array(
	'Giỏ hàng của bạn',
);
?>
<div class="head-tool-title">
 	<div class="main-title"><a href="#">Giỏ hàng của bạn</a></div>
</div>
<?php
if(!isset($products)) 
	$products = Shop::getCartContent();
?>
<?php
if($products) {
	echo '<table class="shopping_cart" width="100%" cellspacing="0" cellpadding="5" border="1" style="border-collapse:collapse; font-size:12px;" bordercolor="#929292">';
	printf('<thead><tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr></thead>',
			Yii::t('shop', 'Image'),
			Yii::t('shop', 'Amount'),
			Yii::t('shop' ,'Product Name'),
			Yii::t('shop', 'Product Price'),
			Yii::t('shop' ,'Sum'),
			''
);
	printf('<tbody>');
	foreach($products as $position => $product) {
		if(@$model = Products::model()->with('category')->findByPk($product['product_id'])) {
			$variations = '';
			if(isset($product['Variations'])) {
				foreach($product['Variations'] as $specification => $variation) {
					$specification = ProductSpecification::model()->findByPk($specification);
					if($specification->is_user_input)
						$variation = $variation[0];
					else
						$variation = ProductVariation::model()->findByPk($variation);

					$variations .= $specification . ': ' . $variation . '<br />';
				}
			}
			$link = Yii::app()->controller->createUrlProductDetail($model->product_id);
			printf('<tr><td>%s</td><td>%s</td><td>%s</td><td class="text-right"><span class="price">%s</span></td><td class="text-right price_'.$position.'"><span class="price">%s</span></td><td>%s</td></tr>',
					'<img width="70" src="'.ShopHelpers::getImageProduct($model->image, 's').'" />',
					CHtml::textField('amount_'.$position,
						$product['amount'], array(
							'size' => 4,
							'class' => 'amount amount_'.$position,
							)
						),
					CHtml::link($model->title,$link),
					Shop::priceFormat($model->getPrice(@$product['Variations'])),
					Shop::priceFormat($model->getPrice(@$product['Variations'], @$product['amount'])),
					CHtml::link(Shop::t('Remove'), array(
							'//shoppingCart/delete',
							'id' => $position), array(
								'confirm' => Yii::t('shop','Are you sure?'),'class'=>'remove')
							)
					);

			Yii::app()->clientScript->registerScript('amount_'.$position,"
					$('.amount_".$position."').keyup(function() {
						$.ajax({
							url:'".$this->createUrl('//shoppingCart/updateAmount')."',
							data: $('#amount_".$position."'),
							success: function(result) {
							$('.amount_".$position."').css('background-color', 'lightgreen');
							$('.widget_amount_".$position."').css('background-color', 'lightgreen');
							$('.widget_amount_".$position."').html($('.amount_".$position."').val());
							$('.price_".$position."').html(result);	
							$('.price_total').load('".$this->createUrl(
							'//shoppingCart/getPriceTotal')."');
							},
							error: function() {
							$('#amount_".$position."').css('background-color', 'red');
							},

							});
				});
					");
			}
}

	if($shippingMethod = Shop::getShippingMethod()) {
		printf('<tr>
				<td></td>
				<td>1</td>
				<td>%s</td>
				<td></td>
				<td class="text-right">%s</td>
				<td class="text-right">%s</td>
				<td>%s</td></tr>',
				Yii::t('shop','Shipping costs'),
				Shop::priceFormat($shippingMethod->price),
				Shop::priceFormat($shippingMethod->price),
				CHtml::link(Shop::t('edit'), array('//shippingMethod/choose'))
				);
	}
echo '<tr>
<td style="text-align: right" colspan="6">
<p class="price_total">'.Shop::getPriceTotal().'</p></td>
</tr>';
printf('</tbody>');
echo '</table>';
?>
<div class="uaction">
	<a href="<?php echo Yii::app()->createUrl('/payment/index');?>"><span>Đặt Hàng</span></a>
	<a href="<?php echo Yii::app()->controller->createUrlPage(80);?>"><span>Hướng dẫn mua hàng</span></a>
</div>
<div class="clear"></div>

<?php

} else echo Yii::t('shop','Your shopping cart is empty'); ?>

